$(function (){

    let sendlock = 0;
    $('.sendbtn').on('click',function (){

        if(sendlock === 1){
            return false;
        }
        let phone = $(".phone").val();
        var regex = /^1\d{10}$/;
        if(regex.test(phone)){
            //发送验证码
            $.ajax({
                type : 'get',
                url : domain + '/api/pb/sendVerify',
                data : {
                    phone : phone
                },
                success : function (res) {
                    if(res.code === 1){
                        setVerifyInterval(60);
                    }else{
                        notice(res.msg, 'error');
                    }
                }
            });
        }else{
            notice('请输入正确的手机号', 'warning');
        }
    });
    
    function setVerifyInterval(seconds) {
        let second = seconds;
        sendlock = 1;
        let id = setInterval(function () {
            $(".sendbtn span").html((second--) + '秒后可重试');
            $(".sendbtn span").css('color', 'red');
        }, 1000);

        setTimeout(function () {
            sendlock = 0;
            clearInterval(id);
            $(".sendbtn span").html('发送验证码').css('color','#00f');
        }, seconds * 1000);
    }

    $(".sublogin").on('click',function () {
        let phone = $(".phone").val();
        var regex = /^1\d{10}$/;
        if(! regex.test(phone)){
            notice('请输入正确的手机号', 'warning');
        }

        let code = $(".code").val();
        regex = /^d{6}$/;

        if(! regex.test(code)){
            notice('请输入验证码', 'warning');
        }

        $.ajax({
            type: 'post',
            url: domain + '/api/pb/login',
            data: {
                phone: phone,
                code: code
            },
            success: function (res) {
                if(res.code === 1){
                    localStorage.setItem('HTTP_TOKEN', res.data.userinfo.token);
                    location.href='home.html';
                }else{
                    notice(res.msg, 'error');
                }
            }
        });

    });
});

function notice(msg, error='succcess'){
    let notice = '<div data-v-5e62a61f="" class="animate__animated '+ error +' animate__fadeOutUp" style="top: 2rem;">\n' +
        '                '+ msg +'.\n' +
        '            </div>';


    $('.messagePop').html(notice).slideDown();

    setTimeout(function () {
        $('.messagePop').html('').slideUp();
    }, 2000);
}